function SDX_Datas(_RootObject)
{
 this.LastTakenID = 0 ;
 this.RootObject = _RootObject ;
 this.GetEvalObjectString = GetEvalObjectString ;
 this.Data = SDX_Data;
 this.InsertData=InsertData;
 this.AcceptChanges = AcceptChanges ;
 this.DeleteCurrentData = DeleteCurrentData ;
 this.InsertModifiedData = InsertModifiedData ;
 this.InsertDeletedData = InsertDeletedData ;
 this.DiffGramXML = DiffGramXML ;
 this.OrginalDatas = new Array() ;
 this.CurrentDatas = new Array() ;
 this.AddedDatas = new Array() ;
 this.ModifiedDatas = new Array() ;
 this.DeletedDatas = new Array() ;
 this.IsInsertedData = IsInsertedData ;
 this.IsModifiedData = IsModifiedData ;
 this.GetRowXML = GetRowXML ;
 this.GetNewID = GetNewID
 this.GetDataByKeyID = GetDataByKeyID;
 this.HasExcludedCharacters = HasExcludedCharacters ;
 function GetDataByKeyID(_DataState, _KeyID)
 {
    if(_DataState == "Current")
    {
        for(var key in this.CurrentDatas)
        {
            if(this.CurrentDatas[key] == null)
                continue;
            if(this.CurrentDatas[key].KeyID == _KeyID)
                return this.CurrentDatas[key];
        }
    }
    return null;
 }
 function GetNewID()
 {
    return this.LastTakenID ++ ;
 }
 function SDX_Data(_RootObject, _ID, _DataKeyID, _Data)
 {
  this.RootObject = _RootObject ;
  this.ID = _ID
  this.KeyID = _DataKeyID
  var datas = new Array() ;
  for(var nX in _Data)
  {
   datas[nX] = _Data[nX] ;
  }
  this.Data = datas;
 }
 function GetEvalObjectString()
 {
  return this.ParentObject.GetEvalObjectString() + ".Rows" ;
 }
 function InsertData(_Data)
 {
  var keyID = this.GetNewID() ;
  this.AddedDatas[keyID]= new this.Data(this.RootObject, this.AddedDatas.length, keyID, this.RootObject.GeneralFunctions.CloneArray(_Data)) ;
  this.CurrentDatas[keyID]= new this.Data(this.RootObject, this.CurrentDatas.length, keyID, this.RootObject.GeneralFunctions.CloneArray(_Data)) ;
  return (keyID) ;
 }
 function AcceptChanges()
 {
  this.OrginalDatas = new Array() ;
  for(var nX in this.CurrentDatas)
  {
   var data = new this.Data(this.CurrentDatas[nX].RootObject, nX, this.CurrentDatas[nX].KeyID, this.RootObject.GeneralFunctions.CloneArray(this.CurrentDatas[nX].Data))
   this.OrginalDatas[nX] = data ;
  }
  this.AddedDatas = new Array() ;
  this.ModifiedDatas = new Array() ;
  this.DeletedDatas = new Array() ;
 }
 function IsInsertedData(_DataKeyID)
 {
   if( this.AddedDatas[_DataKeyID] != null)
   {
    return true ;
   }
  return false ;
 }
 function IsModifiedData(_DataKeyID)
 {
   if( this.ModifiedDatas[_DataKeyID] != null)
   {
    return true ;
   }
  return false ;
 }
 function InsertModifiedData(_DataKeyID)
 {
  if(this.ModifiedDatas[_DataKeyID] != null)
  {
   return this.ModifiedDatas[_DataKeyID] ;
  }
  this.ModifiedDatas[_DataKeyID] = new this.Data(this.RootObject, this.ModifiedDatas.length, _DataKeyID, this.RootObject.GeneralFunctions.CloneArray(this.CurrentDatas[_DataKeyID].Data))
  return this.ModifiedDatas[_DataKeyID] ;
 }
 function InsertDeletedData(_DataKeyID)
 {
  if(this.DeletedDatas[_DataKeyID] != null)
  {
   return this.DeletedDatas[_DataKeyID] ;
  }
  if(this.ModifiedDatas[_DataKeyID] != null)
  {
   this.ModifiedDatas[_DataKeyID] = null ; 
  }
  if(this.AddedDatas[_DataKeyID] != null)
  {
   this.AddedDatas[_DataKeyID] = null ; 
   this.DeleteCurrentData(_DataKeyID) ;
   return ;
  }
  this.DeleteCurrentData(_DataKeyID) ;
  this.DeletedDatas[_DataKeyID] = new this.Data(this.RootObject, this.DeletedDatas.length, _DataKeyID, this.RootObject.GeneralFunctions.CloneArray(this.OrginalDatas[_DataKeyID].Data))
  return this.DeletedDatas[_DataKeyID] ;
 }
 function DeleteCurrentData(_DataKeyID)
 {
  if(this.CurrentDatas[_DataKeyID] != null)
  {
   this.CurrentDatas[_DataKeyID] = null ;
  }
 }
 function GetRowXML(_Datas, _DataIndex)
 {
  var xml = "" ;
  xml += "<SDXGridRowIndex>" + _DataIndex + "</SDXGridRowIndex>"  ;
  for(var nY in this.RootObject.Columns.Columns)
  {
   if(_Datas[_DataIndex] != null)
   {
       if(_Datas[_DataIndex].Data[nY] != "" && _Datas[_DataIndex].Data[nY] != null)
       {
           if(this.RootObject.Columns.Columns[nY].Field == sdxDateField)
           {
            var date = _Datas[_DataIndex].Data[nY]
            var dataString = date.getFullYear() +
                             "-" +
                             ((date.getMonth())>9?(date.getMonth()):"0" + (date.getMonth())) +
                             "-" +
                             ((date.getDate())>9?(date.getDate()):"0" + (date.getDate())) +
                             "T00:00:00%2B00:00"
            xml += "<" + this.RootObject.Columns.Columns[nY].Key + ">" + dataString + "</" + this.RootObject.Columns.Columns[nY].Key + ">"  ;
           }
           else if(HasExcludedCharacters(_Datas[_DataIndex].Data[nY]) == true)
           {
            xml += "<" + this.RootObject.Columns.Columns[nY].Key + ">" + "<![CDATA_BEGIN[" + _Datas[_DataIndex].Data[nY] + "]]END>" + "</" + this.RootObject.Columns.Columns[nY].Key + ">"  ;
           }
           else
           {
            xml += "<" + this.RootObject.Columns.Columns[nY].Key + ">" + _Datas[_DataIndex].Data[nY] + "</" + this.RootObject.Columns.Columns[nY].Key + ">"  ;
           }
       }
   }
//           2005-04-02T00:00:00+01:00
  }
  return xml ;
 }
 function DiffGramXML()
 {
  var DeletedXml = "" ;
  var ModifiedOriginalDataXml = "" ;
  var ModifiedXml = "" ;
  var AddedXml = "" ;
  var ColumnsXml = "" ;
  ColumnsXml += "<Columns>" +
                 this.RootObject.Columns.GetColumnsXML() +
                 "</Columns>"  ;
  
  for(var nX in this.AddedDatas)
  {
   if(this.AddedDatas[nX] == null)
    continue ;
   AddedXml += "<Added>" +
                 this.GetRowXML(this.AddedDatas, nX) +
                 "</Added>"  ;
  }
  for(var nX in this.ModifiedDatas)
  {
   if(this.ModifiedDatas[nX] == null)
    continue ;
   var isInsertedData = this.IsInsertedData(this.ModifiedDatas[nX].KeyID) ;
   if(isInsertedData == false)
   {
    ModifiedOriginalDataXml += "<ModifiedOriginalData>" +
                  this.GetRowXML(this.OrginalDatas, nX) +
                  "</ModifiedOriginalData>" ;
    ModifiedXml += "<Modified>" +
                  this.GetRowXML(this.ModifiedDatas, nX) +
                  "</Modified>" ;
   }
  }
  
  for(var nX in this.DeletedDatas)
  {
    ModifiedOriginalDataXml += "<ModifiedOriginalData>" +
                  this.GetRowXML(this.OrginalDatas, nX) +
                  "</ModifiedOriginalData>" ;
    DeletedXml += "<Deleted>" +
                  this.GetRowXML(this.DeletedDatas, nX) +
                  "</Deleted>" ;
  }
  
  var xml = "<?xml version=\"1.0\" standalone=\"yes\"?>" +
            "<DataSet TableName='" + this.RootObject.DataSetTableName + "'>" +
                ColumnsXml +
                AddedXml +
                ModifiedOriginalDataXml +
                ModifiedXml +
                DeletedXml +
            "</DataSet>" ;
  return xml ;
 }
 function HasExcludedCharacters(compareString)
 {
    if( isNaN( compareString ) == false)
        return false ;
    var excludedCharacters = new Array("\"","<",">","\n") ;
    for(nX=0;nX<excludedCharacters.length;nX++)
    {
        if(compareString.indexOf(excludedCharacters[nX]) != -1)
            return true ;
    } 
    return false ;
 }
}
